using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using static UnityEngine.Rendering.DebugUI;

public class SimpleEnemyAI : EnemyController
{
    [Range(1, 50)]
    [SerializeField] protected float accuracy = 1.0f;
    [SerializeField] protected float rotatePerSecond = 120f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // CD����
        if (_fireCD > 0.0f)
        {
            _fireCD -= Time.deltaTime;
        }
        // ת���޶���
        if (hates != null)
        {
            FaceToTarget(hates);
            if (_fireCD <= 0.0f)
            {
                Fire();
            }
        }
        else
        {
            // ��ҽ�����Ұʱ��Ϊ��޶���
            if (Look())
                Debug.Log("I see you");
        }
    }

    protected bool Look()
    {
        //if(LookAround(transform, Quaternion.identity, viewDistance))
        //    return true;
        //float subAngle = (viewAngle / 2) / accuracy;
        //for(int i = 0; i < accuracy; i++)
        //{
        //    if (LookAround(transform, Quaternion.Euler(0, 0, -1 * subAngle * (i + 1)), viewDistance) || LookAround(transform, Quaternion.Euler(0, 0, subAngle * (i + 1)), viewDistance))
        //        return true;
        //}
        //return false;
        float subAngle = viewAngle / accuracy;
        for (int i = 0; i < accuracy; i++)
            if (LookAround(unit.transform, Quaternion.Euler(0, 0, -viewAngle / 2 + i * subAngle + Mathf.Repeat(rotatePerSecond * Time.time, subAngle)), viewDistance))
                return true;
        return false;
    }

    public bool LookAround(Transform _transform, Quaternion euler, int _viewDistance)
    {
        Debug.DrawRay(_transform.position, euler * _transform.up.normalized * _viewDistance, Color.red);
        RaycastHit2D hit = Physics2D.Raycast(_transform.position, euler * _transform.up.normalized, _viewDistance);
        if (hit.collider.CompareTag("Player"))
        {
            hates = hit.collider.gameObject;
            return true;
        }
        return false;
    }

}
